<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 组合 -->
    <script>
      var combine = (n, k) => {
        let res = []
        var helper = (start, path) => {
          // start是枚举选择的起点 path是当前构建的路径（组合）
          if (path.length == k) {
            res.push(path.slice()) // 拷贝一份path，推入res
            return // 结束当前递归
          }
          for (let i = start; i <= n; i++) {
            // 枚举出所有选择
            path.push(i) // 选择
            helper(i + 1, path) // 向下继续选择
            path.pop() // 撤销选择
          }
        }
        helper(1, []) // 递归的入口，从数字1开始选
        return res
      }
      console.log(combine(4, 2))
    </script>
  </body>
</html>
